package cn.stylefeng.guns.modular.rest.task;

import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.CangGuanRoleId;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.task.entity.TaskDetailed;
import cn.stylefeng.guns.modular.task.entity.TaskOrder;
import cn.stylefeng.guns.modular.task.model.params.TaskDetailedParam;
import cn.stylefeng.guns.modular.task.model.params.TaskOrderParam;
import cn.stylefeng.guns.modular.task.model.result.TaskDetailedResult;
import cn.stylefeng.guns.modular.task.model.result.TaskOrderResult;
import cn.stylefeng.guns.modular.task.service.TaskDetailedService;
import cn.stylefeng.guns.modular.task.service.TaskOrderService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 作业控制器
 */
@Controller
@RequestMapping("/rest/task")
public class restTaskController  extends BaseController{

    @Autowired
    private UserAop userAop;

    @Autowired
    private TaskOrderService taskOrderService;

    /**
     * 查询作业单
     */
    @ResponseBody
    @RequestMapping("/getById")
    public ResponseData getById(TaskOrderParam taskOrderParam ) {
        return ResponseData.success(taskOrderService.getById(taskOrderParam.getId()));
    }

    /**
     * 新增接口
     */
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData addItem(TaskOrderParam taskOrderParam,String type1,String type2,String type3,String type4,String type5,String type6,String type7,String type8,String type9) {
    	User user = userAop.getUser();
    	ResponseData data = new ResponseData();
    	if(taskOrderParam.getTaskWork().equals("卸车")) {
    		taskOrderParam.setV8(type1);
    	}
    	if(taskOrderParam.getTaskWork().equals("装车")) {
    		taskOrderParam.setV8(type2);
    	}
    	if(taskOrderParam.getTaskWork().equals("入库")) {
    		taskOrderParam.setV8(type3);
    	}
    	if(taskOrderParam.getTaskWork().equals("移库")) {
    		taskOrderParam.setV8(type4);
    	}
    	if(taskOrderParam.getTaskWork().equals("领料")) {
    		taskOrderParam.setV8(type5);
    	}
    	if(taskOrderParam.getTaskWork().equals("废料")) {
    		taskOrderParam.setV8(type6);
    	}
    	if(taskOrderParam.getTaskWork().equals("理库")) {
    		taskOrderParam.setV8(type7);
    	}
    	if(taskOrderParam.getTaskWork().equals("批量作业")) {
    		taskOrderParam.setV8(type8);
    	}
    	if(taskOrderParam.getTaskWork().equals("杂工")) {
    		taskOrderParam.setV8(type9);
    	}
    	if(ToolUtil.isNotEmpty(taskOrderParam.getCustomerNumber())) {
    		taskOrderParam.setCustomerNumber("'"+taskOrderParam.getCustomerNumber()+"'");
    	}
    	if (taskOrderParam.getSettlementMode().equals("体积")) {
			if (ToolUtil.isEmpty(taskOrderParam.getVolume())) {
				data.setSuccess(false);
				data.setMessage("体积不得为空");
				return data;
			}
		}
    	if (taskOrderParam.getSettlementMode().equals("数量")) {
			if (ToolUtil.isEmpty(taskOrderParam.getNums())) {
				data.setSuccess(false);
				data.setMessage("数量不得为空");
				return data;
			}
		}
    	if (taskOrderParam.getSettlementMode().equals("重量")) {
			if (ToolUtil.isEmpty(taskOrderParam.getWeights())) {
				data.setSuccess(false);
				data.setMessage("重量不得为空");
				return data;
			}
		}
    	DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");//设置日期格式
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	taskOrderParam.setStatus("未开始");
    	TaskOrderParam to = new TaskOrderParam();
    	to.setV1(DateUtil.getDate());
    	to.setDeptId(deptId);
    	to.setDeliverId(deliverId);
    	int count = taskOrderService.countBydate(to,user.getOperateId());
    	taskOrderParam.setOrderNumber(user.getAccount().substring(0, 3)+LocalDateTime.now().format(fmt)+(count+1));
    	taskOrderParam.setDeptId(deptId);
    	taskOrderParam.setDeliverId(deliverId);
    	taskOrderParam.setV2(DateUtil.getDateS());
    	taskOrderParam.setV3(user.getName());
    	taskOrderParam.setPeoples("0");
    	taskOrderParam.setV6("0");
    	taskOrderParam.setV7(user.getUserId()+"");
        this.taskOrderService.add(taskOrderParam);
        return ResponseData.success();
    }

    /**
     * 作业单据列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TaskOrderParam taskOrderParam,String condition ) {
    	User user = userAop.getUser();
    	taskOrderParam.setDeptId(user.getDeptId());
    	taskOrderParam.setDeliverId(user.getDeliverId());
//		String[] split = user.getRoleId().split(",");//以逗号分割
//		if(CangGuanRoleId.CGRoleId.longValue() != 0l) {
//			for (String string2 : split) {
//				Long cgRole = CangGuanRoleId.CGRoleId;
//				Long userR = Long.parseLong(string2);
//				if (userR.longValue() == cgRole.longValue()) {
//					taskOrderParam.setV7(user.getUserId()+"");
//				}
//			}
//		}
    	LayuiPageInfo LayuiPageInfo = this.taskOrderService.findPageBySpecApplets(taskOrderParam,condition,user.getOperateId());
    	LayuiPageInfo.setCode(200);
        return LayuiPageInfo;
    }

    /**
     * 作业看板
     * @throws ParseException 
     */
    @ResponseBody
    @RequestMapping("/taskreport")
    public ResponseData taskreport(TaskOrderParam taskOrderParam ) throws ParseException {
    	User user = userAop.getUser();
    	taskOrderParam.setDeptId(user.getDeptId());
    	taskOrderParam.setDeliverId(user.getDeliverId());
    	if(taskOrderParam.getStatus().equals("作业中")||taskOrderParam.getStatus().equals("已完成")){
    		taskOrderParam.setCreateTimes(taskOrderParam.getV1());
    		taskOrderParam.setV1("");
    	}
    	List<TaskOrderResult> list = taskOrderService.findtaskreportList(taskOrderParam,user.getOperateId());
    	for (TaskOrderResult r : list) {
    		String endtime = DateUtil.getDateS();
    		if(ToolUtil.isNotEmpty(r.getCreateTimes())) {
    			if(r.getStatus().equals("作业中")) {
    				r.setEndTime(DateUtil.getDatePoorMin(endtime, r.getCreateTimes()));
    			}
    			if(r.getStatus().equals("已完成")) {
    				r.setEndTime(DateUtil.getDatePoorMin(r.getEndTime(), r.getCreateTimes()));
    			}
    		}
		}
        return ResponseData.success(list);
    }

    /**
     * 作废
     */
    @RequestMapping("/invalid")
    @ResponseBody
    public ResponseData invalid(TaskOrderParam taskOrderParam) {
    	User user = userAop.getUser();
    	taskOrderParam.setStatus("作废");
    	taskOrderParam.setV4(DateUtil.getDateS());
    	taskOrderParam.setV5(user.getName());
        this.taskOrderService.update(taskOrderParam);
        return ResponseData.success();
    }

    /**
     * 修改体积、数量、重量
     */
    @RequestMapping("/editTask")
    @ResponseBody
    public ResponseData editTask(TaskOrderParam taskOrderParam) {
    	try {
			double num = Double.parseDouble(taskOrderParam.getNums());
		} catch (Exception e) {
			return ResponseData.error("请正确输入数值");
		}
    	TaskOrderParam param = new TaskOrderParam();
    	param.setId(taskOrderParam.getId());
    	TaskOrder taskOrder = taskOrderService.getById(taskOrderParam.getId());
    	if (taskOrder.getSettlementMode().equals("体积")) {
    		param.setVolume(taskOrderParam.getNums());
		}
    	if (taskOrder.getSettlementMode().equals("数量")) {
    		param.setNums(taskOrderParam.getNums());
		}
    	if (taskOrder.getSettlementMode().equals("重量")) {
    		param.setWeights(taskOrderParam.getNums());
		}
        this.taskOrderService.update(param);
        return ResponseData.success();
    }

    @Autowired
    private TaskDetailedService taskDetailedService;

    /**
     * 查询是否在该作业单据中作业
     */
    @RequestMapping("/findTaskWhether")
    @ResponseBody
    public ResponseData findTaskWhether(@RequestBody TaskOrderParam taskOrderParam) {
    	User user = userAop.getUser();
    	TaskOrder taskOrder = taskOrderService.getById(taskOrderParam.getId());
    	if (taskOrder.getStatus().equals("已完成")||taskOrder.getStatus().equals("作废")) {
    		return ResponseData.error("该作业单已完成或不存在");
		}
		TaskDetailedParam taskDetailedParam = new TaskDetailedParam();
		taskDetailedParam.setUserId(user.getUserId());
		taskDetailedParam.setStatus("作业中");
		taskDetailedParam.setTaskId(Long.parseLong(taskOrderParam.getId().toString()));
		taskDetailedParam.setStartTime(DateUtil.getDate());
		TaskDetailedResult taskDetailedResult = taskDetailedService.findBySpecZuoYeZhong(taskDetailedParam);
		if (taskDetailedResult != null) {
			return ResponseData.error("已在其它作业中，不可同时加入多个作业");
		}
    	TaskDetailedParam taskDetailedParam2 = new TaskDetailedParam();
    	taskDetailedParam2.setUserId(user.getUserId());
    	taskDetailedParam2.setTaskId(Long.parseLong(taskOrder.getId().toString()));
    	taskDetailedParam2.setStatus("作业中");
        return ResponseData.success(taskDetailedService.findBySpec(taskDetailedParam2));
    }
    
    /**
     * 加入作业
     */
    @RequestMapping("/join")
    @ResponseBody
    public ResponseData join(@RequestBody TaskOrderParam taskOrderParam) {
    	User user = userAop.getUser();
    	TaskOrder taskOrder = taskOrderService.getById(taskOrderParam.getId());
    	if (taskOrder.getStatus().equals("未开始")) {
    		taskOrderParam.setCreateTimes(DateUtil.getDateS());
    		taskOrderParam.setCreateName(user.getName());
    		taskOrderParam.setStatus("作业中");
		} else if (taskOrder.getStatus().equals("已完成")||taskOrder.getStatus().equals("作废")) {
    		return ResponseData.error("该作业单已完成或不存在");
		}
    	TaskDetailedParam taskDetailedParam = new TaskDetailedParam();
    	taskDetailedParam.setTaskId(Long.parseLong(taskOrder.getId().toString()));
    	taskDetailedParam.setUserId(user.getUserId());
    	TaskDetailedResult taskDetailedResult = taskDetailedService.findBySpec(taskDetailedParam);
    	if (taskDetailedResult == null) {
        	int ps = Integer.parseInt(taskOrder.getPeoples());//增加作业单  人数
        	ps = ps +1;
        	taskOrderParam.setPeoples(ps+""); 
		} 
    	taskDetailedParam.setStatus("作业中");//新增作业明细
    	taskDetailedParam.setOrderNumber(taskOrder.getOrderNumber());
    	taskDetailedParam.setCustomerNumber(taskOrder.getCustomerNumber());
    	taskDetailedParam.setSettlementMode(taskOrder.getSettlementMode());
    	taskDetailedParam.setUserName(user.getName());
    	taskDetailedParam.setStartTime(DateUtil.getDateS());
    	taskDetailedParam.setDeptId(user.getDeptId());
    	taskDetailedParam.setDeliverId(user.getDeliverId());
    	taskDetailedService.add(taskDetailedParam);

    	QueryWrapper<TaskDetailed> queryWrapper = new QueryWrapper<TaskDetailed>();//更新实时人数
    	queryWrapper.eq("status", "作业中");
    	queryWrapper.eq("task_id", taskOrder.getId());
		int count = taskDetailedService.count(queryWrapper);
		taskOrderParam.setV6(count+"");
    	taskOrderService.update(taskOrderParam);
		
        return ResponseData.success();
    }

    /**
     * 已完成修改体积、数量、重量
     */
    @RequestMapping("/editTaskYwc")
    @ResponseBody
    public ResponseData editTaskYwc(TaskOrderParam taskOrderParam) {
    	try {
			double num = Double.parseDouble(taskOrderParam.getNums());
		} catch (Exception e) {
			return ResponseData.error("请正确输入数值");
		}
    	TaskOrderParam param = new TaskOrderParam();
    	param.setId(taskOrderParam.getId());
    	TaskOrder taskOrder = taskOrderService.getById(taskOrderParam.getId());
    	if (!taskOrder.getStatus().equals("已完成")) {
    		return ResponseData.error("该作业单状态不是已完成");
		}
    	BigDecimal num = new BigDecimal(taskOrderParam.getNums());//总量
    	BigDecimal hours = new BigDecimal(taskOrder.getHours());//工时
    	BigDecimal prescription = new BigDecimal(0);//时效
    	BigDecimal as = new BigDecimal(0);
    	TaskDetailedParam taskDetailedParam2 = new TaskDetailedParam();
    	taskDetailedParam2.setTaskId(Long.parseLong(taskOrder.getId().toString()));
    	List<TaskDetailedResult> list = taskDetailedService.findListBySpec(taskDetailedParam2);
    	
    	if(taskOrder.getSettlementMode().equals("体积")) {//计算时效
        	param.setVolume(num.toString());
    		if (list.size() >0) {
    			if (ToolUtil.isNotEmpty(taskOrder.getVolume())) {
    				if(num.compareTo(as)==1) {
    					prescription = num.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
    					TaskDetailedParam taskDetailedParam = new TaskDetailedParam();
        				for (TaskDetailedResult r : list) {
        					BigDecimal h = new BigDecimal(r.getHours());
    						r.setVolume(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
    						ToolUtil.copyProperties(r, taskDetailedParam);
    						taskDetailedService.update(taskDetailedParam);
    					}
    				}
    			}
			}
    	}
    	if(taskOrder.getSettlementMode().equals("数量")) {//计算时效
        	param.setNums(num.toString());
    		if (list.size() >0) {
    			if (ToolUtil.isNotEmpty(taskOrder.getNums())) {
					TaskDetailedParam taskDetailedParam = new TaskDetailedParam();
    				if(num.compareTo(as)==1) {
    					prescription = num.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
        					BigDecimal h = new BigDecimal(r.getHours());
    						r.setNums(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
    						ToolUtil.copyProperties(r, taskDetailedParam);
    						taskDetailedService.update(taskDetailedParam);
    					}
    				}
    			}
			}
    	}
    	if(taskOrder.getSettlementMode().equals("重量")) {//计算时效
        	param.setWeights(num.toString());
    		if (list.size() >0) {
    			if (ToolUtil.isNotEmpty(taskOrder.getWeights())) {
					TaskDetailedParam taskDetailedParam = new TaskDetailedParam();
    				if(num.compareTo(as)==1) {
    					prescription = num.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
        					BigDecimal h = new BigDecimal(r.getHours());
    						r.setWeights(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
    						ToolUtil.copyProperties(r, taskDetailedParam);
    						taskDetailedService.update(taskDetailedParam);
    					}
    				}
    			}
			}
    	}
    	
    	param.setPrescription(prescription.toString());
        this.taskOrderService.update(param);
        return ResponseData.success();
    }
    
    /**
     * 退出作业
     * @throws ParseException 
     */
    @RequestMapping("/out")
    @ResponseBody
    public ResponseData out(@RequestBody TaskOrderParam taskOrderParam) throws ParseException {
    	User user = userAop.getUser();
    	TaskOrder taskOrder = taskOrderService.getById(taskOrderParam.getId());
    	if (!taskOrder.getStatus().equals("作业中")) {
    		return ResponseData.error("该作业单不在作业中");
		}
    	TaskDetailedParam taskDetailedParam = new TaskDetailedParam();
    	taskDetailedParam.setStatus("作业中");
    	taskDetailedParam.setTaskId(Long.parseLong(taskOrder.getId().toString()));
    	taskDetailedParam.setUserId(user.getUserId());
    	TaskDetailedResult taskDetailedResult = taskDetailedService.findBySpec(taskDetailedParam);
    	if (taskDetailedResult != null) {
    		String EndTime = DateUtil.getDateS();
    		taskDetailedParam.setId(taskDetailedResult.getId());
    		taskDetailedParam.setEndTime(EndTime);
    		taskDetailedParam.setStatus("已完成");
    		taskDetailedParam.setHours(DateUtil.getDatePoorMin(EndTime, taskDetailedResult.getStartTime()));
    		taskDetailedService.update(taskDetailedParam);
		}
    	
    	QueryWrapper<TaskDetailed> queryWrapper = new QueryWrapper<TaskDetailed>();//判断如果没有人作业   修改作业单完成
    	queryWrapper.eq("status", "作业中");
    	queryWrapper.eq("task_id", taskOrder.getId());
		int count = taskDetailedService.count(queryWrapper);
		if (count == 0) {
	    	String EndTime = DateUtil.getDateS();
	    	BigDecimal as = new BigDecimal(0);
	    	taskOrderParam.setStatus("已完成");
	    	taskOrderParam.setEndTime(EndTime);
	    	taskOrderParam.setEndName(user.getName());
	    	
	    	if (!taskOrder.getStatus().equals("作业中")) {
	    		return ResponseData.error("该作业单不在作业中");
			}
	    	BigDecimal hours = new BigDecimal(0);//工时
	    	BigDecimal prescription = new BigDecimal(0);//时效
	    	TaskDetailedParam taskDetailedParam2 = new TaskDetailedParam();
	    	taskDetailedParam2.setTaskId(Long.parseLong(taskOrder.getId().toString()));
	    	List<TaskDetailedResult> list = taskDetailedService.findListBySpec(taskDetailedParam2);
	    	for (TaskDetailedResult r : list) {//合计总工时
				if (r.getStatus().equals("作业中")) {
					r.setStatus("已完成");
					r.setEndTime(EndTime);
					BigDecimal h = new BigDecimal(DateUtil.getDatePoorMin(EndTime, r.getStartTime()));//计算个人工时
					hours = hours.add(h);
					r.setHours(h.toString());
				} else {
					BigDecimal h = new BigDecimal(r.getHours());
					hours = hours.add(h);
				}
			}
	    	
	    	if(taskOrder.getSettlementMode().equals("体积")) {//计算时效
	    		if (list.size() >0) {
	    			if (ToolUtil.isNotEmpty(taskOrder.getVolume())) {
	    				BigDecimal volume = new BigDecimal(taskOrder.getVolume());
	    				if(volume.compareTo(as)==1) {
	    					prescription = volume.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
	        				for (TaskDetailedResult r : list) {
	        					BigDecimal h = new BigDecimal(r.getHours());
	    						r.setVolume(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
	    						ToolUtil.copyProperties(r, taskDetailedParam);
	    						taskDetailedService.update(taskDetailedParam);
	    					}
	    				}
	    			}
				}
	    	}
	    	if(taskOrder.getSettlementMode().equals("数量")) {//计算时效
	    		if (list.size() >0) {
	    			if (ToolUtil.isNotEmpty(taskOrder.getNums())) {
	    				BigDecimal nums = new BigDecimal(taskOrder.getNums());
	    				if(nums.compareTo(as)==1) {
	    					prescription = nums.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
	        				for (TaskDetailedResult r : list) {
	        					BigDecimal h = new BigDecimal(r.getHours());
	    						r.setNums(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
	    						ToolUtil.copyProperties(r, taskDetailedParam);
	    						taskDetailedService.update(taskDetailedParam);
	    					}
	    				}
	    			}
				}
	    	}
	    	if(taskOrder.getSettlementMode().equals("重量")) {//计算时效
	    		if (list.size() >0) {
	    			if (ToolUtil.isNotEmpty(taskOrder.getWeights())) {
	    				BigDecimal weights = new BigDecimal(taskOrder.getWeights());
	    				if(weights.compareTo(as)==1) {
	    					prescription = weights.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
	        				for (TaskDetailedResult r : list) {
	        					BigDecimal h = new BigDecimal(r.getHours());
	    						r.setWeights(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
	    						ToolUtil.copyProperties(r, taskDetailedParam);
	    						taskDetailedService.update(taskDetailedParam);
	    					}
	    				}
	    			}
				}
	    	}
	    	
	    	taskOrderParam.setHours(hours.toString());//更新作业单
	    	taskOrderParam.setPrescription(prescription.toString());
		}
		taskOrderParam.setV6(count+"");
    	taskOrderService.update(taskOrderParam);
        return ResponseData.success();
    }
    
    /**
     * 完成作业
     * @throws ParseException 
     */
    @RequestMapping("/complete")
    @ResponseBody
    public ResponseData complete(TaskOrderParam taskOrderParam) throws ParseException {
    	User user = userAop.getUser();
    	String EndTime = DateUtil.getDateS();
    	if(ToolUtil.isNotEmpty(taskOrderParam.getEndTime())) {
    		EndTime = taskOrderParam.getEndTime();
    	}
    	BigDecimal as = new BigDecimal(0);
    	taskOrderParam.setStatus("已完成");
    	taskOrderParam.setEndTime(EndTime);
    	taskOrderParam.setEndName(user.getName());
    	
    	TaskOrder taskOrder = taskOrderService.getById(taskOrderParam.getId());
    	if (!taskOrder.getStatus().equals("作业中")) {
    		return ResponseData.error("该作业单不在作业中");
		}
    	BigDecimal hours = new BigDecimal(0);//工时
    	BigDecimal prescription = new BigDecimal(0);//时效
    	TaskDetailedParam taskDetailedParam = new TaskDetailedParam();
    	taskDetailedParam.setTaskId(Long.parseLong(taskOrder.getId().toString()));
    	List<TaskDetailedResult> list = taskDetailedService.findListBySpec(taskDetailedParam);
    	for (TaskDetailedResult r : list) {//合计总工时
			if (r.getStatus().equals("作业中")) {
				r.setStatus("已完成");
				r.setEndTime(EndTime);
				BigDecimal h = new BigDecimal(DateUtil.getDatePoorMin(EndTime, r.getStartTime()));//计算个人工时
				hours = hours.add(h);
				r.setHours(h.toString());
				if(taskOrder.getSettlementMode().equals("工时")) {
					ToolUtil.copyProperties(r, taskDetailedParam);
					taskDetailedService.update(taskDetailedParam);
				}
			} else {
				BigDecimal h = new BigDecimal(r.getHours());
				hours = hours.add(h);
			}
		}
    	
    	if(taskOrder.getSettlementMode().equals("体积")) {//计算时效
    		if (list.size() >0) {
    			if (ToolUtil.isNotEmpty(taskOrder.getVolume())) {
    				BigDecimal volume = new BigDecimal(taskOrder.getVolume());
    				if(volume.compareTo(as)==1) {
    					prescription = volume.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
        					BigDecimal h = new BigDecimal(r.getHours());
    						r.setVolume(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
    						ToolUtil.copyProperties(r, taskDetailedParam);
    						taskDetailedService.update(taskDetailedParam);
    					}
    				}
    			}
			}
    	}
    	if(taskOrder.getSettlementMode().equals("数量")) {//计算时效
    		if (list.size() >0) {
    			if (ToolUtil.isNotEmpty(taskOrder.getNums())) {
    				BigDecimal nums = new BigDecimal(taskOrder.getNums());
    				if(nums.compareTo(as)==1) {
    					prescription = nums.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
        					BigDecimal h = new BigDecimal(r.getHours());
    						r.setNums(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
    						ToolUtil.copyProperties(r, taskDetailedParam);
    						taskDetailedService.update(taskDetailedParam);
    					}
    				}
    			}
			}
    	}
    	if(taskOrder.getSettlementMode().equals("重量")) {//计算时效
    		if (list.size() >0) {
    			if (ToolUtil.isNotEmpty(taskOrder.getWeights())) {
    				BigDecimal weights = new BigDecimal(taskOrder.getWeights());
    				if(weights.compareTo(as)==1) {
    					prescription = weights.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
        					BigDecimal h = new BigDecimal(r.getHours());
    						r.setWeights(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
    						ToolUtil.copyProperties(r, taskDetailedParam);
    						taskDetailedService.update(taskDetailedParam);
    					}
    				}
    			}
			}
    	}
    	
    	taskOrderParam.setHours(hours.toString());//更新作业单
    	taskOrderParam.setPrescription(prescription.toString());
		taskOrderParam.setV6("0");
    	taskOrderService.update(taskOrderParam);
        return ResponseData.success();
    }
    
    /**
     * 拆分明细
     * @throws ParseException 
     */
    @RequestMapping("/chaimingxi")
    @ResponseBody
    public ResponseData chaimingxi(String nums1,String nums2,String nums3,Long taskId) throws ParseException {
    	TaskDetailedParam taskDetailedParam = new TaskDetailedParam();
    	TaskOrder taskOrder = taskOrderService.getById(taskId);
    	taskDetailedParam.setTaskId(taskId);
    	List<TaskDetailedResult> list = taskDetailedService.findListBySpec(taskDetailedParam);
    	List<TaskDetailedParam> list2 = new ArrayList<TaskDetailedParam>();

    	BigDecimal as = new BigDecimal(0);
    	int a = 0;
    	if(ToolUtil.isNotEmpty(nums1)) {
        	BigDecimal nums1b = new BigDecimal(nums1);
    		if(nums1b.compareTo(as) == 1) {
    			a++;
    		}
    	}
    	if(ToolUtil.isNotEmpty(nums2)) {
        	BigDecimal nums2b = new BigDecimal(nums2);
    		if(nums2b.compareTo(as) == 1) {
    			a++;
    		}
    	}
    	if(ToolUtil.isNotEmpty(nums3)) {
        	BigDecimal nums3b = new BigDecimal(nums3);
    		if(nums3b.compareTo(as) == 1) {
    			a++;
    		}
    	}
    	if(a < 2) {
    		return ResponseData.error("必须拆为两个或以上");
    	}
    	BigDecimal dd = new BigDecimal(a);
    	BigDecimal hours = new BigDecimal(taskOrder.getHours());//工时
    	
    	if(taskOrder.getSettlementMode().equals("体积")) {//计算时效
    		if (list.size() >0) {
    			if (ToolUtil.isNotEmpty(nums1)) {
    				BigDecimal volume = new BigDecimal(nums1);
    				if(volume.compareTo(as)==1) {
    					BigDecimal prescription = volume.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
    						TaskDetailedParam detailedParam = new TaskDetailedParam();
    						ToolUtil.copyProperties(r, detailedParam);
        					BigDecimal h = new BigDecimal(r.getHours());
        					detailedParam.setVolume(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        					detailedParam.setDetaileds("纸品");
        					BigDecimal newh = h.divide(dd,2,BigDecimal.ROUND_HALF_DOWN);
        					detailedParam.setHours(newh.toString());
    						detailedParam.setId(null);
    						list2.add(detailedParam);
    					}
    				}
    			}
    			if (ToolUtil.isNotEmpty(nums2)) {
    				BigDecimal volume = new BigDecimal(nums2);
    				if(volume.compareTo(as)==1) {
    					BigDecimal prescription = volume.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
    						TaskDetailedParam detailedParam = new TaskDetailedParam();
    						ToolUtil.copyProperties(r, detailedParam);
        					BigDecimal h = new BigDecimal(r.getHours());
        					detailedParam.setVolume(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        					detailedParam.setDetaileds("卫品");
        					BigDecimal newh = h.divide(dd,2,BigDecimal.ROUND_HALF_DOWN);
        					detailedParam.setHours(newh.toString());
    						detailedParam.setId(null);
    						list2.add(detailedParam);
    					}
    				}
    			}
    			if (ToolUtil.isNotEmpty(nums3)) {
    				BigDecimal volume = new BigDecimal(nums3);
    				if(volume.compareTo(as)==1) {
    					BigDecimal prescription = volume.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
    						TaskDetailedParam detailedParam = new TaskDetailedParam();
    						ToolUtil.copyProperties(r, detailedParam);
        					BigDecimal h = new BigDecimal(r.getHours());
        					detailedParam.setVolume(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        					detailedParam.setDetaileds("湿巾");
        					BigDecimal newh = h.divide(dd,2,BigDecimal.ROUND_HALF_DOWN);
        					detailedParam.setHours(newh.toString());
    						detailedParam.setId(null);
    						list2.add(detailedParam);
    					}
    				}
    			}
			}
    		for (TaskDetailedResult r : list) {
    	    	TaskDetailedParam dp = new TaskDetailedParam();
    	    	dp.setId(r.getId());
    			taskDetailedService.delete(dp);
			}
    		for (TaskDetailedParam p : list2) {
    			taskDetailedService.add(p);
			}
    	}
    	if(taskOrder.getSettlementMode().equals("数量")) {//计算时效
    		if (list.size() >0) {
    			if (ToolUtil.isNotEmpty(nums1)) {
    				BigDecimal nums = new BigDecimal(nums1);
    				if(nums.compareTo(as)==1) {
    					BigDecimal prescription = nums.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
    						TaskDetailedParam detailedParam = new TaskDetailedParam();
    						ToolUtil.copyProperties(r, detailedParam);
        					BigDecimal h = new BigDecimal(r.getHours());
        					detailedParam.setNums(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        					detailedParam.setDetaileds("纸品");
        					BigDecimal newh = h.divide(dd,2,BigDecimal.ROUND_HALF_DOWN);
        					detailedParam.setHours(newh.toString());
    						detailedParam.setId(null);
    						list2.add(detailedParam);
    					}
    				}
    			}
    			if (ToolUtil.isNotEmpty(nums2)) {
    				BigDecimal nums = new BigDecimal(nums2);
    				if(nums.compareTo(as)==1) {
    					BigDecimal prescription = nums.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
    						TaskDetailedParam detailedParam = new TaskDetailedParam();
    						ToolUtil.copyProperties(r, detailedParam);
        					BigDecimal h = new BigDecimal(r.getHours());
        					detailedParam.setNums(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        					detailedParam.setDetaileds("卫品");
        					BigDecimal newh = h.divide(dd,2,BigDecimal.ROUND_HALF_DOWN);
        					detailedParam.setHours(newh.toString());
    						detailedParam.setId(null);
    						list2.add(detailedParam);
    					}
    				}
    			}
    			if (ToolUtil.isNotEmpty(nums3)) {
    				BigDecimal nums = new BigDecimal(nums3);
    				if(nums.compareTo(as)==1) {
    					BigDecimal prescription = nums.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
    						TaskDetailedParam detailedParam = new TaskDetailedParam();
    						ToolUtil.copyProperties(r, detailedParam);
        					BigDecimal h = new BigDecimal(r.getHours());
        					detailedParam.setNums(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        					detailedParam.setDetaileds("湿巾");
        					BigDecimal newh = h.divide(dd,2,BigDecimal.ROUND_HALF_DOWN);
        					detailedParam.setHours(newh.toString());
    						detailedParam.setId(null);
    						list2.add(detailedParam);
    					}
    				}
    			}
			}
    		for (TaskDetailedResult r : list) {
    	    	TaskDetailedParam dp = new TaskDetailedParam();
    	    	dp.setId(r.getId());
    			taskDetailedService.delete(dp);
			}
    		for (TaskDetailedParam p : list2) {
    			taskDetailedService.add(p);
			}
    	}
    	if(taskOrder.getSettlementMode().equals("重量")) {//计算时效
    		if (list.size() >0) {
    			if (ToolUtil.isNotEmpty(nums1)) {
    				BigDecimal weights = new BigDecimal(nums1);
    				if(weights.compareTo(as)==1) {
    					BigDecimal prescription = weights.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
    						TaskDetailedParam detailedParam = new TaskDetailedParam();
    						ToolUtil.copyProperties(r, detailedParam);
        					BigDecimal h = new BigDecimal(r.getHours());
        					detailedParam.setWeights(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        					detailedParam.setDetaileds("纸品");
        					BigDecimal newh = h.divide(dd,2,BigDecimal.ROUND_HALF_DOWN);
        					detailedParam.setHours(newh.toString());
    						detailedParam.setId(null);
    						list2.add(detailedParam);
    					}
    				}
    			}
    			if (ToolUtil.isNotEmpty(nums2)) {
    				BigDecimal weights = new BigDecimal(nums2);
    				if(weights.compareTo(as)==1) {
    					BigDecimal prescription = weights.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
    						TaskDetailedParam detailedParam = new TaskDetailedParam();
    						ToolUtil.copyProperties(r, detailedParam);
        					BigDecimal h = new BigDecimal(r.getHours());
        					detailedParam.setWeights(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        					detailedParam.setDetaileds("卫品");
        					BigDecimal newh = h.divide(dd,2,BigDecimal.ROUND_HALF_DOWN);
        					detailedParam.setHours(newh.toString());
    						detailedParam.setId(null);
    						list2.add(detailedParam);
    					}
    				}
    			}
    			if (ToolUtil.isNotEmpty(nums3)) {
    				BigDecimal weights = new BigDecimal(nums3);
    				if(weights.compareTo(as)==1) {
    					BigDecimal prescription = weights.divide(hours,2,BigDecimal.ROUND_HALF_DOWN);
        				for (TaskDetailedResult r : list) {
    						TaskDetailedParam detailedParam = new TaskDetailedParam();
    						ToolUtil.copyProperties(r, detailedParam);
        					BigDecimal h = new BigDecimal(r.getHours());
        					detailedParam.setWeights(prescription.multiply(h).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
        					detailedParam.setDetaileds("湿巾");
        					BigDecimal newh = h.divide(dd,2,BigDecimal.ROUND_HALF_DOWN);
        					detailedParam.setHours(newh.toString());
    						detailedParam.setId(null);
    						list2.add(detailedParam);
    					}
    				}
    			}
			}
    		for (TaskDetailedResult r : list) {
    	    	TaskDetailedParam dp = new TaskDetailedParam();
    	    	dp.setId(r.getId());
    			taskDetailedService.delete(dp);
			}
    		for (TaskDetailedParam p : list2) {
    			taskDetailedService.add(p);
			}
    	}
    	TaskOrderParam orderParam = new TaskOrderParam();
    	orderParam.setId(taskOrder.getId());
    	orderParam.setV10("已拆");
    	taskOrderService.update(orderParam);
    	
        return ResponseData.success();
    }

}
